﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp0
{
    public partial class ku_ru : Form
    {
        string mid;
        public ku_ru()
        {
            InitializeComponent();
        }
        public ku_ru(string a)
        {
            this.mid = a;
           

            InitializeComponent();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "" || textBox2.Text == "" )
            {
                MessageBox.Show("输入有空, 请检查", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);

            }
            else
            { 
                try
                {//textBox1.Text=输入 存放位置     textBox2.Text=入库数量
             
                    Dao dao = new Dao();
                    string sql = $"insert into InDepot values('{mid}','{textBox1.Text}',{textBox2.Text},'{DateTime.Now.ToString("yyyyMMddHHmmss")}')";
                    int n = dao.Execute(sql);
                
                    if (n >= 1)
                    {
                    

                    }
                    Dao dao1 = new Dao();
                    SqlCommand cmd = dao1.command(sql);
                    cmd.CommandText = $"SELECT COUNT(*) FROM Depot where Mid ='{mid}'and Iplace ='{textBox1.Text}'";
                    Int32 count = (Int32)cmd.ExecuteScalar();
                    string a = count.ToString();
                
                    if (count >= 1)
                    {
                      
                        string sql11 = $"update Depot set Dstock=Dstock+{textBox2.Text} where Mid='{mid}' and Iplace ='{textBox1.Text}'";
                        int l = dao.Execute(sql11);

                        if (l >= 1)
                        {
                            MessageBox.Show("入库成功");

                        }

                    }
                    else
                    {
                     
                        string sql11 = $"insert into Depot values('{mid}','{textBox1.Text}',{textBox2.Text})";
                        int l = dao.Execute(sql11);

                        if (l >= 1)
                        {
                            MessageBox.Show("入库成功");

                        }
                    }

                }
                catch
                {
                    MessageBox.Show("添加失败");
                }

            }
        }

        private void ku_ru_Load(object sender, EventArgs e)
        {

        }
    }
}
